#include "../include/administrator.h"
#include "../include/menu.h"
#include "../include/trips.h"
#include "../include/longin.h"

//创建头节点
ADM *admHead(ADM *head)
{
    head = (ADM *)malloc(sizeof(ADM));
    if(head == NULL)
    {
        perror("malloc head");
        exit(1);//申请空间失败
    }
    memset(head, 0, sizeof(ADM));

    return head;
}

//默认管理员账号
ADM *defaultAdm(ADM *head)
{
    assert(head);
    ADM *node = NULL;
    ADM *end = head;
       
    //创建新节点
    node = (ADM *)malloc(sizeof(ADM));
    if(node == NULL)
    {
        perror("malloc deflult adm");
        exit(1);
    }
    memset(node, 0, sizeof(ADM));
 
    //默认管理员和密码
    strcpy(node->adm.name, "admin");
    strcpy(node->adm.password, "123456");

    end = head;
    //遍历到链表最后
    while(end->next != NULL)
    {
        end = end->next;
    }

    //将新节点链接到已有的链表中
    end->next = node;
    node->next = NULL;

    return head;
}

//创建新管理员(尾插)
ADM *newAdm(ADM *head)
{
    assert(head);
    ADM *node = NULL;
    ADM *end = head;
       
    char strname[20] = {0};
    //输入用户名
    printf("输入用户名：");
    scanf("%s", strname);

    //判断管理员是否存在
    while(end->next != NULL)
    {
        if(!strcmp(end->next->adm.name, strname))
        {
            printf("管理员已存在！\n");
            printf("请前往登录！\n");
            logMessage(LOG_WARNING, "NewAdm already exists!");
            break;
        }
        end = end->next;
    }
    if(end->next == NULL)
    {
        //创建新节点
        node = (ADM *)malloc(sizeof(ADM));
        if(node == NULL)
        {
            perror("malloc new adm");
            exit(1);
        }
        memset(node, 0, sizeof(ADM));

        strcpy(node->adm.name,strname);
        printf("输入密码：");
        scanf("%s", node->adm.password);
        //将新节点链接到已有的链表中
        end->next = node;
        node->next = NULL;
        logMessage(LOG_INFO, "addNewAdm %s -- %s", node->adm.name, node->adm.password);  
        printf("注册成功！\n");
    }

    return head;
}

//管理员登录
ADM *admLogin(ADM *head)
{
    assert(head);
    ADM *end = head->next;
    char str[20] = {0};
    int count = 0;
    int i = 2;
    printf("请输入用户名：");
    scanf("%s", str);
    while(end != NULL)
    {
        if(!strcmp(end->adm.name, str))
        { 
            while(i >= 0)
            {
                printf("请输入密码：");
                scanf("%s", str);
                if(!strcmp(end->adm.password, str))
                {
                    printf("登录成功！\n");
                    logMessage(LOG_INFO, "admLongin %s -- %s", end->adm.name, end->adm.password);

                    return end;
                }
                else if(i > 0)
                {
                    printf("输入错误！\n还有%d次机会\n", i);
                    i--;
                }
                else  
                {
                    i--;
                }  
            }
        }
        end = end->next;
    }
    if(count == 0 && i == 2)
    {
        printf("管理员不存！\n");
        logMessage(LOG_ERROR, " No adm!");

        return NULL;
    }
    if(end == NULL)
    {
        printf("登录失败！\n");
        logMessage(LOG_ERROR, "admLongin error!");

        return NULL;
    }
}

//管理员打印
void admprint(ADM *head)
{
    assert(head);
    ADM *end = head;
    printf("%s-----%s\n", "账号", "密码");
    printf("%s--%s\n", end->adm.name, end->adm.password);
}

//管理员改密码
ADM *admPwd(ADM *head)
{
    assert(head);
    ADM *end = head->next;
    char passwd[20] = {0};
    char username[20] = {0};
    printf("请输入你的用户名：");
    scanf("%s", username);
    if(!strcmp("admin", username))
    {
        printf("默认管理员密码无法更改！\n");

        return head;
    }
    while (end != NULL)
    {
        if (!strcmp(end->adm.name, username))
        {
            printf("请输入你的原密码：");
            scanf("%s", passwd);
            if (!strcmp(passwd, end->adm.password))
            {
                printf("请输入你要改的密码：");
                scanf("%s", passwd);
                char ch[7] = {0};
                admchrand(ch);
                char str[7] = {0};
	            printf("请输入验证码：");
	            scanf("%s", str);
                admchcmp(str);
                admchcmp(ch);
                if(!strcmp(str, ch))
                {
                    strcpy(end->adm.password, passwd);
                    printf("密码更改成功！\n");
                    logMessage(LOG_INFO, "admChangepwd %s -- %s", end->adm.name, end->adm.password);

                    return head;
                }
                else
                {
                    printf("验证码输入有误！\n");
                    printf("修改密码失败！\n");
                    logMessage(LOG_ERROR, "admChangepwd error!");

                    return head;
                }
            }
            else
            {
                printf("输入密码有误!\n");
                logMessage(LOG_ERROR, "admChangepwd error!");
                
                return head;
            }
        }
        end = end->next;
    }
    if(end == NULL)
    {
        printf("管理员不存在！\n");
        logMessage(LOG_ERROR, "admChangepwd Noadm!");
    }

    return head;
}

//释放链表
int freeadm(ADM *head)
{
    if(head->next == NULL)
    {
        freeadm(head->next);
        free(head);
        head = NULL;
    }  

    return 0;
}

//生成验证码
char* admchrand(char* ch)
{
	srand(time(NULL));

	int i = 0;
	int a, b, c, d;

	while(i < 6)
	{
		a = 48 + rand() % 10;
		b = 65 + rand() % 26;
		c = 97 + rand() % 26;
		d = 1 +  rand() % 10;

		if(d <= 3)
		{
			ch[i] = a;
		}
		else if(d <= 6)
		{
			ch[i] = b;
		}
		else
		{
			ch[i] = c;
		}

		i++;
	}
	ch[6] = '\0';
	printf("验证码：");
	printf("%s\n", ch);

	return ch;
}

//验证码改大小写
void admchcmp(char* str)
{
	while(*str != '\0')
	{
		if(*str >= 'a' && *str <= 'z')
		{
			*str = *str - 32;
		}
		*str++;
	}
}